package com.aidex.biz.controller;

import com.aidex.biz.domain.CapBill;
import com.aidex.biz.domain.vo.OrgTypeStatisticVo;
import com.aidex.biz.service.CapBillService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资方结算Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/biz/bizCapBill")
public class CapBillController extends BaseController {
    @Autowired
    private CapBillService capBillService;

    /**
     * 查询资方结算列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:list')")
    @GetMapping("/list")
    public R<PageInfo> list(CapBill capBill, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            capBill.setCompanyId(null);
        } else {
            capBill.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        capBill.setPage(new PageDomain(request, response));
        return R.data(capBillService.findPage(capBill));
    }


    /**
     * 获取资方结算详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:query')")
    @GetMapping(value = "/{id}")
    public R<CapBill> detail(@PathVariable("id") String id) {
        return R.data(capBillService.get(id));
    }

    /**
     * 新增资方结算
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:add')")
    @Log(title = "资方结算", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated CapBill capBill) {
        return R.status(capBillService.save(capBill));
    }

    /**
     * 修改资方结算
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:edit')")
    @Log(title = "资方结算", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CapBill capBill) {
        if ("1".equals(capBill.getStatus())) {
            if ("2".equals(capBill.getType())) {
                capBill.setAmount(capBill.getFkAmount().multiply(capBill.getLv()));
            }
        }
        return R.status(capBillService.save(capBill));
    }


    /**
     * 删除资方结算
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:remove')")
    @Log(title = "资方结算", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(capBillService.deleteCapBillByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(capBillService.findMaxSort(new CapBill()));
    }

    /**
     * 导出资方结算列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:export')")
    @Log(title = "资方结算", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CapBill capBill) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            capBill.setCompanyId(null);
        } else {
            capBill.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<CapBill> list = capBillService.findList(capBill);
        ExcelUtil<CapBill> util = new ExcelUtil<CapBill>(CapBill.class);
        return util.exportExcel(list, "资方结算数据");
    }


    /**
     * 费用统计
     *
     * @param capBill
     * @return
     */
    @Log(title = "机构费用统计", businessType = BusinessType.SELECT)
    @GetMapping("/orgTypeStatistic")
    public R<PageInfo> orgTypeStatistic(CapBill capBill, HttpServletRequest request, HttpServletResponse response) {
        capBill.setPage(new PageDomain(request, response));
        return R.data(capBillService.orgTypeStatistic(capBill));
    }

    /**
     * 费用统计 合计
     *
     * @param capBill
     * @return
     */
    @Log(title = "费用统计(合计)", businessType = BusinessType.SELECT)
    @GetMapping("/sumOrgTypeStatistic")
    public R sumOrgTypeStatistic(CapBill capBill, HttpServletRequest request, HttpServletResponse response) {
        capBill.setPage(new PageDomain(request, response));
        return R.data(capBillService.sumOrgTypeStatistic(capBill));
    }


    @Log(title = "费用统计", businessType = BusinessType.EXPORT)
    @GetMapping("/exportOrgTypeStatistic")
    public R exportOrgTypeStatistic(CapBill capBill) {
        List<OrgTypeStatisticVo> list = capBillService.ExportOrgTypeStatistic(capBill);
        ExcelUtil<OrgTypeStatisticVo> util = new ExcelUtil<OrgTypeStatisticVo>(OrgTypeStatisticVo.class);
        return util.exportExcel(list, "费用统计");
    }

    @Log(title = "产品费用统计", businessType = BusinessType.SELECT)
    @GetMapping("/orgTypeDetailStatistic")
    public R<List<OrgTypeStatisticVo>> orgTypeDetailStatistic(CapBill capBill) {
        return R.data(capBillService.orgTypeDetailStatistic(capBill));
    }


    /**
     * 费用统计
     *
     * @param capBill
     * @return
     */
    @Log(title = "费用统计列表", businessType = BusinessType.SELECT)
    @GetMapping("/orgTypeDetailStatisticRecords")
    public R<PageInfo> orgTypeDetailStatisticRecords(CapBill capBill, HttpServletRequest request, HttpServletResponse response) {
        capBill.setPage(new PageDomain(request, response));
        return R.data(capBillService.orgTypeDetailStatisticRecords(capBill));
    }

}
